﻿// ***********************************************************************
// Project			: Micua
// Assembly         : Micua.DAL
// Author           : iceStone
// Created          : 2013-11-18 11:55
//
// Last Modified By : iceStone
// Last Modified On : 2013-11-18 19:04
// ***********************************************************************
// <copyright file="Factory.cs" company="Wedn.Net">
//     Copyright (c) Wedn.Net. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Data.Entity;
using System.Runtime.Remoting.Messaging;
using Micua.IDAL;
using Micua.Model;

namespace Micua.DAL
{
    /// <summary>
    /// 数据库上下文工厂类
    /// </summary>
    /// <remarks>
    ///  2013-11-18 19:04 Created By iceStone
    /// </remarks>
    public static class Factory
    {
        /// <summary>
        /// 取当前线程内唯一的数据库上下文
        /// </summary>
        /// <remarks>
        ///  2013-11-18 19:04 Created By iceStone
        /// </remarks>
        /// <returns>数据库上下文</returns>
        public static DbContext GetCurrentDbContext()
        {
            //NB的单例模式，跨时代的产品！（线程内实例唯一）
            var db = CallContext.GetData(DALStruct.CurrentDbContextKey) as DbContext;

            if (db != null) return db;
            db = new MicuaModelContainer();
            CallContext.SetData(DALStruct.CurrentDbContextKey, db);
            return db;
        }
    }
}
